package com.company;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/*
    实际应用场景水果和活动均为数据库数据
    添加商品和添加活动从数据库增加后调用方法
    可扩展
 */

public class Main {

    public static void main(String[] args) throws IOException {
        Fruit apple = new Fruit("apple",new BigDecimal(8));//模拟苹果实体类
        Fruit caoMei = new Fruit("caoMei",new BigDecimal(13));//模拟草莓实体类
        List<Fruit> fruits = new ArrayList<>();
        apple.setNum(new BigDecimal(10));//10斤苹果
        caoMei.setNum(new BigDecimal(10));//10斤草莓
        fruits.add(apple);
        fruits.add(caoMei);

        List<Event> events = new ArrayList<>();//活动列表

        //问题1
        BigDecimal answer1 = buyFruit(fruits,events);
        System.out.println(answer1);//输出210.00

        //问题2
        Fruit mangGuo = new Fruit("mangGuo",new BigDecimal(20));//模拟芒果实体类
        mangGuo.setNum(new BigDecimal(10));//10斤芒果
        fruits.add(mangGuo);
        BigDecimal answer2 = buyFruit(fruits,events);
        System.out.println(answer2);//输出410.00


        //问题3
        Event event = new Event();//草莓折扣0.8活动
        event.setFruitNames("caoMei");
        event.setType("zk");
        event.setDisCount(new BigDecimal(0.8));
        events.add(event);
        BigDecimal answer3 = buyFruit(fruits,events);
        System.out.println(answer3);//输出384.00

        //问题4
        Event mjEvent = new Event();//满减活动满100-10,不可叠加
        mjEvent.setType("mj");
        mjEvent.setAmount(new BigDecimal(100));
        mjEvent.setSubAmount(new BigDecimal(10));
        events.add(mjEvent);
        BigDecimal answer4 = buyFruit(fruits,events);
        System.out.println(answer4);

    }



    /*
    计算价格方法，通过传入的水果和创建的活动计算实付金额
     */
    public static BigDecimal buyFruit(List<Fruit> fruits,List<Event> events){
        BigDecimal amount = BigDecimal.ZERO;
        boolean isZk = false;//防止重复配置多次折扣
        Event mjEvent = null;//满减活动
        //计算折扣
        for (Fruit fruit:fruits) {
            amount = amount.add(fruit.getPrice().multiply(fruit.getNum()));
            for (Event event:events) {
                mjEvent = "mj".equals(event.getType()) ? event : null;
                if("zk".equals(event.getType()) && event.getFruitNames().equals(fruit.getName())){
                    //配置折扣商品且未参加过折扣且折扣范围在0到1之间则参与折扣
                    if(!isZk
                            && event.getDisCount().compareTo(BigDecimal.ZERO)>0 && event.getDisCount().compareTo(new BigDecimal(1))<0){
                        amount = amount.subtract(fruit.getPrice().multiply(fruit.getNum()).multiply(new BigDecimal(1).subtract(event.getDisCount())));
                        isZk = true;
                    }
                }
            }
        }
        //计算满减
        if(mjEvent != null && mjEvent.getAmount().compareTo(mjEvent.getSubAmount())>0 && amount.compareTo(mjEvent.getAmount())>0){
            amount = amount.subtract(mjEvent.getSubAmount());
        }

        return amount.setScale(2,BigDecimal.ROUND_HALF_UP);
    }

}
